﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IWshRuntimeLibrary;
using File = System.IO.File;

namespace LaunchMeNot.Core
{
    /// <summary>
    /// A registry entry
    /// </summary>
    public class RegistryShortcut : StartupEntryBase
    {
        protected string _originalName;

        /// <summary>
        /// Gets the original name of the entry (used as a registry key).
        /// </summary>
        /// <value>The original name of the entry (used as a registry key).</value>
        public string OriginalName
        {
            get { return _originalName; }
            private set
            {
                _originalName = value;
                OnPropertyChanged("OriginalName");
            }
        }

        public RegistryShortcut() 
            :base(){
        }

        /// <summary>
        /// Initializes a <see cref="ShellShortcut"/> from an existing file.
        /// If the file is a shortcut (.lnk) file, it's properties are used. Otherwise, the files name, path and arguments are parsed.
        /// </summary>
        /// <param name="fileName">Name of file from which to initialize this entry.</param>
        public RegistryShortcut(string fileName)
            : base() {
            this.Name = System.IO.Path.GetFileNameWithoutExtension(fileName);
            var pair = Utils.SplitArguments(fileName);
            this.Path = pair.Key;
            this.Arguments = pair.Value;
        }

        /// <summary>
        /// Initializes a new <see cref="ShellShortcut"/>.
        /// </summary>
        /// <param name="name">The friendly name.</param>
        /// <param name="path">The path of the file to launch.</param>
        /// <param name="arguments">The arguments.</param>
        public RegistryShortcut(string name, string path, string arguments)
            : this() {
            this.Name = name;
            this.Path = path;
            this.Arguments = arguments;
            _originalName = name;
        }

        /// <summary>
        /// Initializes a new <see cref="RegistryShortcut"/>.
        /// </summary>
        /// <param name="entry">The entry.</param>
        public RegistryShortcut(IStartupEntry entry)
            : this() {
            this.Name = entry.Name;
            this.Path = entry.Path;
            this.Arguments = entry.Arguments;
            //_originalName = entry.Name;
        }

        /// <summary>
        /// Clones this instance.
        /// </summary>
        /// <returns>A deep copy of this entry.</returns>
        public override IStartupEntry Clone()
        {
            var entry = new RegistryShortcut()
            {
                Name = this.Name,
                Path = this.Path,
                Arguments = this.Arguments,
                EntryType = this.EntryType,
                 OriginalName = this.OriginalName
            };
            return entry;
        }


        public override string ToString()
        {
            return string.Format("{0} - {1}", Name ?? string.Empty, Path ?? string.Empty);
        }
    }
}
